package com.tanhua.app.service;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSON;
import com.tanhua.api.db.QuestionApi;
import com.tanhua.api.db.UserInfoApi;
import com.tanhua.api.mongdb.RecommentApi;
import com.tanhua.domain.UserHolder;
import com.tanhua.domain.db.Question;
import com.tanhua.domain.db.UserInfo;
import com.tanhua.domain.mogo.RecommendUser;
import com.tanhua.domain.vo.PageBeanVo;
import com.tanhua.domain.vo.TodayBest;
import com.tanhua.template.HuanXinTemplate;
import org.apache.commons.lang3.RandomUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class RecommentService {

    @Reference
    private RecommentApi recommentApi;

    @Reference
    private UserInfoApi userInfoApi;

    @Reference
    private QuestionApi questionApi;

    @Autowired
    private HuanXinTemplate huanXinTemplate;

    public ResponseEntity findMaxScore(Long id) {
        //1 最高缘分值的佳人
        RecommendUser recommendUser = recommentApi.findMaxScore(id);

        //        如果没有推荐用户，应该给一个默认的用户
        if(recommendUser==null){
            recommendUser = new RecommendUser();
            recommendUser.setUserId(2L); //在这里是写死的  你可以做成随机的
            recommendUser.setScore(60.0); //这里的得分也是写死的
        }
        //2 带着最高缘分值佳人的id去查询mysql数据获取userInfo数据
        UserInfo userInfo = userInfoApi.findByUser(recommendUser.getUserId());
        //3 自定义一个app需要的vo进行封装
        TodayBest vo = new TodayBest();
        BeanUtils.copyProperties(userInfo,vo);
        vo.setTags(userInfo.getTags().split(","));
        vo.setFateValue(recommendUser.getScore().longValue());
        //4 将封装好的vo返回给app
        return ResponseEntity.ok(vo);
    }


    public ResponseEntity queryTodayBestList(Integer page, Integer pagesize) {
        //1 分页查询佳人的id+缘分值 （recommentUser）
        PageBeanVo pb=recommentApi.queryTodayBestList(UserHolder.findUser().getId(),page,pagesize);

        //2 带着佳人的UserId去查询UserInfo表
        List<RecommendUser> items = (List<RecommendUser>) pb.getItems();

        //没推荐的佳人
        // 如果该登录用户没有推荐人（自己造）
        if(items.size()==0){
            items=fromtuijianren();
        }
        //有推荐佳人
        List<TodayBest> todayBestList=new ArrayList<TodayBest>();
        if(CollectionUtil.isNotEmpty(items)){
            for (RecommendUser recommendUser : items) {
                UserInfo userInfo = userInfoApi.findByUser(recommendUser.getUserId());
                //3 recommentUser+UserInfo=TodayBest
                TodayBest vo = new TodayBest();
                BeanUtils.copyProperties(userInfo,vo);
                vo.setTags(userInfo.getTags().split(","));
                vo.setFateValue(recommendUser.getScore().longValue());
                todayBestList.add(vo);
            }
        }
        //4 将TodayBest给pageBean  pageBean<List<todayBest>>
        pb.setItems(todayBestList);

        return ResponseEntity.ok(pb);

    }

    //给当前没有推荐佳人的用户一些假数据
    private List<RecommendUser> fromtuijianren() {
        List<RecommendUser> list=new ArrayList<RecommendUser>();

        for(Long i=1l;i<=10;i++){
            RecommendUser recommendUser = new RecommendUser();
            recommendUser.setUserId(i);
            recommendUser.setScore(RandomUtils.nextDouble(60,99));
            list.add(recommendUser);
        }
        return list;
    }


    public ResponseEntity personalInfo(Long jrid) {
        // 1 根据jrid查询userInfo数据
        UserInfo userInfo = userInfoApi.findByUser(jrid);
        //2 根据jrid查询 当前登录用户和该佳人的缘分值
        Long score=recommentApi.findScore(UserHolder.findUser().getId(),jrid);
        if(score==null){
            score=99L;
        }
        //3 封装TodayBest做响应
        TodayBest vo = new TodayBest();
        BeanUtils.copyProperties(userInfo,vo);
        vo.setTags(userInfo.getTags().split(","));
        vo.setFateValue(score);
        return ResponseEntity.ok(vo);
    }

    public ResponseEntity strangerQuestions(Long userId) {
        Question question = questionApi.findByQuestion(userId);
        if(question==null){
            question=new Question();
            question.setStrangerQuestion("你是GG还是MM?");
        }
        return ResponseEntity.ok(question.getStrangerQuestion());
    }

    public ResponseEntity toStrangerQuestions(Integer jrId, String info) {
        //组装环信要的数据
        Map<String,String> xuanxinMap=new HashMap<String,String>();
        //查询当前登录用户
        UserInfo userInfo = userInfoApi.findByUser(UserHolder.findUser().getId());
        xuanxinMap.put("userId",userInfo.getId().toString());
        xuanxinMap.put("nickname",userInfo.getNickname());
        //查询佳人的问题
        Question question = questionApi.findByQuestion(jrId.longValue());
        xuanxinMap.put("strangerQuestion",question.getStrangerQuestion());
        xuanxinMap.put("reply",info);
        //参数1：给谁发
        //参数2：发的内容 json
        String xuanxinMap_json= JSON.toJSONString(xuanxinMap);
        huanXinTemplate.sendMsg(jrId.toString(),xuanxinMap_json);
        return ResponseEntity.ok(null);
    }
}
